package framework;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

public class Util {
	
	public static Animation getFlippedAnimation(Animation toFlip, boolean horizontalFlip, boolean verticalFlip) {
		final int COUNT = toFlip.getFrameCount();
		Image[] flipedFrames = new Image[COUNT];
		Animation flippedAnimation = new Animation();

		for(int i=0; i < COUNT; i++) {
			flipedFrames[i] = toFlip.getImage(i).getFlippedCopy(horizontalFlip, verticalFlip);
			flipedFrames[i].setAlpha(toFlip.getImage(i).getAlpha());
			flippedAnimation.addFrame(flipedFrames[i], toFlip.getDuration(i));
		}
		return flippedAnimation;
	}
	
	public static Rectangle2D getCollision(BasicEntity element1, BasicEntity element2) {
		Rectangle r1 = element1.getHitBox();
		Rectangle r2 = element2.getHitBox();
		java.awt.Rectangle rect1 = new java.awt.Rectangle((int)r1.getX(), (int)r1.getY(), (int)r1.getWidth(), (int)r1.getHeight());
		java.awt.Rectangle rect2 = new java.awt.Rectangle((int)r2.getX(), (int)r2.getY(), (int)r2.getWidth(), (int)r2.getHeight());
		return rect1.createIntersection(rect2);
	}
	
	public static int getDuration(Animation anim) {
		int[] durations = anim.getDurations();
		int sum = 0;
		for(int duration : durations) {
			sum += duration;
		}
		return sum;
	}
	
	public static int randomize(int from, int to) {
		return (int) Math.floor(Math.random() * (to - from + 1)) + from;
	}
	
	public static float getScale(GameContainer gc) {
		return gc.getWidth() / 1280f;
	}

}
